﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAMNETLibrary.Models;
using DAMNETLibrary.Utilities;
using DAMNETLibrary.Resources;
using System.Transactions;
using System.Web;

namespace DAMNETLibrary.Controllers
{
    public class UserController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User Login(string emailAddress, string password)
        {
            User user = null;
            SimpleAES aes = new SimpleAES();
            string pass = aes.EncryptToString(password);
            string email = aes.EncryptToString(emailAddress);

            using (DAMNETDataContext db = new DAMNETDataContext())
            {
                user = db.Users.Where(u => u.EmailAddress == email && u.Password == pass).FirstOrDefault();

                if (user == null)
                    throw new DAMNETException(Errors.UserNotFound);
            }

            return user;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static User GetUser()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[Settings.UserInformationCookieName];

            if (cookie == null)
                return null;

            SimpleAES aes = new SimpleAES();
            string email = aes.EncryptToString(cookie["email"]);

            DAMNETDataContext db = new DAMNETDataContext();
            return db.Users.Where(u => u.EmailAddress == email).FirstOrDefault();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static User GetUser(string emailAddress)
        {
            SimpleAES aes = new SimpleAES();
            string email = aes.EncryptToString(emailAddress);

            DAMNETDataContext db = new DAMNETDataContext();
            return db.Users.Where(u => u.EmailAddress == email).FirstOrDefault();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="response"></param>
        public static bool CreateSession(User user, HttpResponse response)
        {
            try
            {
                SimpleAES aes = new SimpleAES();
                HttpCookie cookie = new HttpCookie(Settings.UserInformationCookieName);
                cookie["email"] = aes.DecryptString(user.EmailAddress);

                cookie.Expires = DateTime.Now.AddDays(1);

                response.Cookies.Add(cookie);
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="response"></param>
        public static void DestorySession(HttpRequest request, HttpResponse response)
        {
            HttpCookie cookie = request.Cookies[Settings.UserInformationCookieName];

            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
                response.Cookies.Add(cookie);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        public static User ValidateSession()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[Settings.UserInformationCookieName];
            HttpResponse response = HttpContext.Current.Response;

            if (cookie == null || !cookie.HasKeys)
                return null;
            else
            {
                try
                {
                    User user = GetUser(cookie["email"]);

                    if (user != null)
                    {
                        cookie.Expires = DateTime.Now.AddDays(1);
                        response.Cookies.Add(cookie);
                        return user;
                    }
                }
                catch
                {
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    return null;
                }
            }

            cookie.Expires = DateTime.Now.AddDays(-1);
            return null;
        }
    }
}